package com.wsg.bindsysapp;

import android.app.ActivityManager;
import android.os.Build;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.jaredrummler.android.processes.ProcessManager;
import com.jaredrummler.android.processes.models.AndroidAppProcess;

import java.util.Collections;
import java.util.List;

public class GetTaskActivity extends AppCompatActivity {

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            getTask();
            handler.sendEmptyMessageDelayed(0, 2000);
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_get_task);

        handler.sendEmptyMessageDelayed(0, 2000);

        findViewById(R.id.btn_get_task).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getTask();
            }
        });
    }


    public void getTask() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { //For versions less than lollipop
            Log.e("info", "小于  21...........................");
            ActivityManager am = ((ActivityManager) getSystemService(ACTIVITY_SERVICE));
            List<ActivityManager.RunningTaskInfo> taskInfo = am.getRunningTasks(5);
            String top = taskInfo.get(0).topActivity.getPackageName();
            Log.e("info", "top app = " + top);
        } else { //For versions Lollipop and above
            Log.e("info", "大于  21...........................");
            List<AndroidAppProcess> processes = ProcessManager.getRunningForegroundApps(getApplicationContext());

            Collections.sort(processes, new ProcessManager.ProcessComparator());
            String tasks = "";
            String top = "";
            for (int i = 0; i <= processes.size() - 1; i++) {
                tasks += processes.get(i).name + "\n";
                if (processes.get(i).name.equalsIgnoreCase("com.google.android.gms")) { //always the package name above/below this package is the top app
                    if ((i + 1) <= processes.size() - 1) { //If processes.get(i+1) available, then that app is the top app
                        top = processes.get(i + 1).name;
                    } else if (i != 0) { //If the last package name is "com.google.android.gms" then the package name above this is the top app
                        top = processes.get(i - 1).name;
                    } else {
                        if (i == processes.size() - 1) { //If only one package name available
                            top = processes.get(i).name;
                        }
                    }
                    Log.e("info", "top app = " + top);
                }
            }
            Toast.makeText(this, tasks, Toast.LENGTH_SHORT).show();
        }
    }


}
